package com.hzit.ms.controller;

import com.hzit.ms.resp.RespVo;
import com.hzit.ms.service.IOrderService;
import com.hzit.ms.service.ISkillService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * 秒杀接口
 */
@RestController
@RequestMapping("/sk")
public class SkillController {

    private Logger logger = LoggerFactory.getLogger(SkillController.class);

    @Autowired
    private ValueOperations<String,Object> valueOperations;

    @Autowired
    private ISkillService skillService;

    @Autowired
    private IOrderService orderService;


    /**
     * 第一步：生成隐藏路径，随机生成一个uuid作为路径
     * @return
     */
    @RequestMapping("/path/{userId}/{goodsId}")
    public RespVo<String> createHiddenUrl(@PathVariable("userId") String userId,
                                          @PathVariable("goodsId") String goodsId){
        logger.info("用户id：{}生成随机地址：{}",userId);
        String path = UUID.randomUUID().toString().replace("-","");
       //放到redis
        String redisKey = "sk:path:"+userId+"_"+goodsId;
        valueOperations.set(redisKey,path,120, TimeUnit.SECONDS);
        RespVo<String> respVo = new RespVo<String>();
        respVo.setData(path);
        respVo.setCode(0);
        respVo.setMsg("成功");
        return respVo;

    }

    /**
     * 第二步：开始秒杀
     * @param ,path
     * @return
     */
    @RequestMapping("/{path}/getGoods/{goodsId}/{userId}")
    public RespVo  skGoods(@PathVariable("path") String path,
                           @PathVariable("goodsId") String goodsId,
                           @PathVariable("userId") String userId){
        logger.info("#######用户id：{}开始秒杀商品：{}，路径：{}#######",userId,goodsId,path);

        RespVo respVo = new RespVo();

        if(StringUtils.isEmpty(userId)){
            respVo.setMsg("用户id为空");
            respVo.setCode(-1);
            logger.info("用户id为空");
            return respVo;
        }

        if(StringUtils.isEmpty(goodsId)){
            respVo.setMsg("商品id为空");
            respVo.setCode(-1);
            logger.info("商品id为空");
            return respVo;
        }

        if(StringUtils.isEmpty(path)){
            respVo.setMsg("缺少path参数");
            respVo.setCode(-1);
            logger.info("缺少path参数");
            return respVo;
        }

        String redisKey = "sk:path:"+userId+"_"+goodsId;

        String redisPath = (String) valueOperations.get(redisKey);

        if(!path.equals(redisPath)){
            respVo.setMsg("非法请求");
            respVo.setCode(-1);
            logger.info("非法请求，隐藏路径不正确");
            return respVo;

        }

        respVo =  skillService.getSkGoods(userId,goodsId);

        return respVo;
    }

    /**
     * 根据用户id和商品id查询秒杀结果
     * @param userId
     * @param goodsId
     */
    public RespVo querySkResult(String userId,String goodsId){


        return null;
    }


}
